package com.beichuan_travel.service.impl;

import com.beichuan_travel.enums.ResultCode;
import com.beichuan_travel.enums.Sort;
import com.beichuan_travel.exception.ApiException;
import com.beichuan_travel.interfaces.transform.HotelCovertBasic;
import com.beichuan_travel.mapper.HotelMapper;
import com.beichuan_travel.model.dto.HotelDTO;
import com.beichuan_travel.model.po.AttractionPO;
import com.beichuan_travel.model.po.HotelPO;
import com.beichuan_travel.model.vo.HotelVO;
import com.beichuan_travel.model.vo.ResultVO;
import com.beichuan_travel.service.HotelService;
import com.beichuan_travel.utils.ArraysUtils;
import org.apache.ibatis.annotations.Param;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service("hotelService")
public class HotelServiceImpl implements HotelService {

    @Resource
    HotelMapper hotelMapper;

    @Resource
    ArraysUtils arraysUtils;


    @Override
    public HotelVO queryByName(String name) {
        HotelPO po = hotelMapper.queryByName(name);
        HotelVO vo = new HotelVO();
        vo.setId(po.getId());
        vo.setName(po.getName());
        vo.setLocation(po.getLocation());
        vo.setType(po.getType());
        vo.setDescription(po.getDescription());
        vo.setLevel(po.getLevel());
        vo.setImgUrl(hotelMapper.imgUrl(po.getId()));

        return vo;
    }

    @Override
    public List<HotelVO> queryByPrice(Integer lower, Integer upper) {
        List<HotelPO> list = hotelMapper.queryByPrice(lower, upper);
        if (list == null && list.isEmpty()) {
            return null;
        }
        List<HotelVO> hotelVOList = HotelCovertBasic.INSTANCE.POtoCoertVO(list);
        return hotelVOList;
    }

    @Override
    public List<HotelVO> queryByStar(Integer star) {
        List<HotelPO> list = hotelMapper.queryByStar(star, star + 1);
        if (list == null && list.isEmpty()) {
            return null;
        }
        List<HotelVO> hotelVOList = HotelCovertBasic.INSTANCE.POtoCoertVO(list);
        return hotelVOList;
    }

    @Override
    public List<HotelVO> queryAllDescByStar(Integer type) {
        List<HotelPO> list = hotelMapper.queryAllByStarDesc();
        if (list == null && list.isEmpty()) {
            return null;
        }
        if (type != Sort.ASC.getSort() && type != Sort.DESC.getSort()) {
            throw new ApiException(ResultCode.VALIDATE_FAILED, "请传入数字0或数字1");
        }
        if (type == Sort.ASC.getSort()) {
            arraysUtils.reverse(list);
        }
        List<HotelVO> hotelVOList = HotelCovertBasic.INSTANCE.POtoCoertVO(list);
        return hotelVOList;
    }

    @Override
    public List<HotelVO> queryAllASCByPrice(Integer type) {
        List<HotelPO> list = hotelMapper.queryAllByPriceAsc();
        if (list == null && list.isEmpty()) {
            return null;
        }
        if (type != Sort.ASC.getSort() && type != Sort.DESC.getSort()) {
            throw new ApiException(ResultCode.VALIDATE_FAILED, "请传入数字0或数字1");
        }
        if (type == Sort.DESC.getSort()) {
            arraysUtils.reverse(list);
        }
        List<HotelVO> hotelVOList = HotelCovertBasic.INSTANCE.POtoCoertVO(list);
        return hotelVOList;
    }

    @Override
    public List<HotelVO> fuzzyQueryName(String name) {
        List<HotelPO> attractionPOList = hotelMapper.queryByLikeName("%"+name+"%");
        if (attractionPOList == null && attractionPOList.isEmpty()) {
            return null;
        }
        List<HotelVO> hotelVOList = HotelCovertBasic.INSTANCE.POtoCoertVO(attractionPOList);
        return hotelVOList;
    }

    @Override
    public List<HotelVO> queryAllByName(Integer type) {
        List<HotelPO> POList = hotelMapper.queryAllByNameAsc();
        if (POList == null && POList.isEmpty()) {
            return null;
        }
        if (type != Sort.ASC.getSort() && type != Sort.DESC.getSort()) {
            throw new ApiException(ResultCode.VALIDATE_FAILED, "请传入数字0或数字1");
        }
        if (type == Sort.DESC.getSort()) {
            arraysUtils.reverse(POList);
        }
        List<HotelVO> VOList = HotelCovertBasic.INSTANCE.POtoCoertVO(POList);
        return VOList;
    }

    @Override
    public int addHotel(HotelDTO hotelDTO) {
        HotelPO hotelPO = new HotelPO();

        return hotelMapper.addHotel(hotelPO);
    }

    @Override
    public List<HotelVO> queryByConditionForRelax(String location, String livingTime, Integer price, Integer roomType) {
        return null;
    }

    @Override
    public List<HotelVO> queryByConditionForBusiness(String location, String livingTime, Integer price, Integer roomType) {
        return null;
    }
}
